<?php
require dirname(dirname(__FILE__)) . '/include/basefunc.php';
if (!isset($argv[1])){
	print_log('Parameter error!');
}

require dirname(dirname(__FILE__)) . '/include/conf.php';
require dirname(dirname(__FILE__)) . '/include/database.php';

$db = db_touzhu();
$time_start_sql = date('Y-m-d H:i:s', time() - 3600 * 2);
$sql = "select a.id, a.from, a.competition_id, a.competition, a.match_id, a.home_team_id, a.home_team, a.visiting_team_id, a.visiting_team,";
$sql .= "a.result_score, a.add_time, a.status, b.id as hid, b.type, b.playing_time, b.pankou, b.over, b.under, b.over_betid, b.under_betid, b.fetch_time ";
$sql .= "from match_status as a left join half_compare as b on b.match_id=a.match_id and a.`from`=b.`from` where a.`from`=2 and ";
$sql .= "a.status=3 and a.add_time>='{$time_start_sql}' and b.type<7 and b.model_num=0";
$re = $db->query($sql);

if (!empty($re)){
	$nowtime = date('Y-m-d H:i:s', time());
	$compare_arr = array();
	foreach ($re as $per){
		$key_match = $per['match_id'];
		$compare_arr[$key_match]['hid_'.$per['type']] = $per['hid'];
		$compare_arr[$key_match]['type_'.$per['type']] = $per['type'];
		$compare_arr[$key_match]['pankou_'.$per['type']] = $per['pankou'];
		$compare_arr[$key_match]['over_'.$per['type']] = $per['over'];
		$compare_arr[$key_match]['under_'.$per['type']] = $per['under'];
		$compare_arr[$key_match]['over_betid_'.$per['type']] = $per['over_betid'];
		$compare_arr[$key_match]['under_betid_'.$per['type']] = $per['under_betid'];
	}
	
	if ($compare_arr){
		$up_model_arr = array();
		foreach ($compare_arr as $per){
			if (isset($per['pankou_1']) && isset($per['pankou_4']) && !empty($per['pankou_1']) && !empty($per['pankou_4'])){
				$model_num = apply_bet_model($per['pankou_1'], $per['over_1'], $per['pankou_3'], $per['over_3'], $per['pankou_4'], $per['over_4']);
				
				if ($model_num !== false){
					$sql_money = "select bet_money, conclusion from bet_model where `number`={$model_num} and status=0 limit 1";
					$money_re = $db->query($sql_money);
					$bet_money = isset($money_re[0]['bet_money']) ? $money_re[0]['bet_money'] : 0;
					$conclusion = isset($money_re[0]['conclusion']) ? $money_re[0]['conclusion'] : '';
					$up_model_sql = "update half_compare set model_num={$model_num}, bet_money={$bet_money}, conclusion='{$conclusion}', unread=1 where id={$per['hid_4']}";
					$up_re = $db->query($up_model_sql);
					if (!$up_re){
						print_log($up_model_sql, false);
					}
					
					$bet_id = ($conclusion == '大球') ? $per['over_betid_4'] : $per['under_betid_4'];
					$auto_bet_add = "insert into auto_bet (bet_id, bet_money, add_time) values('{$bet_id}', {$bet_money}, '{$nowtime}')";
					$add_re = $db->query($auto_bet_add);
					if (!$add_re){
						print_log($auto_bet_add);
					}
				}
			}
		}
	}
}

function apply_bet_model($pankou_start, $over_start, $pankou_half_start, $over_half_start, $pankou_half_end, $over_half_end){
	//1 比赛开始时间盘口为2.5球，抓取时间2盘口为1球
	if ($pankou_start == 2.5 && $pankou_half_end == 1){
		return 1;
	}
	
	//3 比赛开始时间盘口为2.25球，抓取时间2盘口为4.25球
	if ($pankou_start == 2.25 && $pankou_half_end == 4.25){
		return 3;
	}
	
	//4 比赛开始时间盘口为3球，水位大于0.9小于1，抓取时间2盘口为2.5球
	if ($pankou_start == 3 && $pankou_half_end == 2.5 && $over_start < 1 && $over_start > 0.9){
		return 4;
	}
	
	//7 比赛开始时间盘口为3.25球，水位大于等于1，抓取时间2盘口为1.5球
	if ($pankou_start == 3.25 && $pankou_half_end == 1.5 && $over_start >= 1){
		return 7;
	}
	
	//8 比赛开始时间盘口为3.25球，抓取时间2盘口为3球
	if ($pankou_start == 3.25 && $pankou_half_end == 3){
		return 8;
	}
	
	//9 比赛开始时间盘口为3.5球，抓取时间2盘口为3球
	if ($pankou_start == 3.5 && $pankou_half_end == 3){
		return 9;
	}
	
	//12 比赛开始时间盘口为2.5球，抓取时间2盘口为5.25球
	if ($pankou_start == 2.5 && $pankou_half_end == 5.25){
		return 12;
	}
	
	//13 比赛开始时间盘口为2.75球，水位小于0.9，抓取时间2盘口为2.5球，水位大于1
	if ($pankou_start == 2.75 && $pankou_half_end == 2.5 && $over_start < 0.9 && $over_half_end > 1){
		return 13;
	}
	
	//14 比赛开始时间盘口为2.75球，水位小于0.9，抓取时间2盘口为3.5球，水位大于1
	if ($pankou_start == 2.75 && $pankou_half_end == 3.5 && $over_start < 0.9 && $over_half_end > 1){
		return 14;
	}
	
	//16 比赛开始时间盘口为2.75球，抓取时间2盘口为5.5球
	if ($pankou_start == 2.75 && $pankou_half_end == 5.5){
		return 16;
	}
	
	//17比赛开始时间盘口为3球，水位大于1，抓取时间2盘口为3.75球
	if ($pankou_start == 3 && $pankou_half_end == 3.75 && $over_start > 1){
		return 17;
	}
	
	//18比赛开始时间盘口为2.5球，水位小于0.9，抓取时间2盘口为2.5球，水位小于0.9
	if ($pankou_start == 2.5 && $pankou_half_end == 2.5 && $over_start < 0.9 && $over_half_end < 0.9){
		return 18;
	}
	
	//19比赛开始时间盘口为3.25，抓取时间1盘口为3，抓取时间2盘口为2.75，买大球
	if ($pankou_start == 3.25 && $pankou_half_start == 3 && $pankou_half_end < 2.75){
		return 19;
	}
	
	//20比赛开始时间盘口为3.5，抓取时间1盘口为3，抓取时间2盘口为2.75，买大球
	if ($pankou_start == 3.5 && $pankou_half_start == 3 && $pankou_half_end < 2.75){
		return 20;
	}
	
	/*
	//2 比赛开始时间盘口为2.25球，抓取时间2盘口为4.5球
	if ($pankou_start == 2.25 && $pankou_half_end == 4.5){
		return 2;
	}
	
	//5 比赛开始时间盘口为3球，水位大于1，抓取时间2盘口为2.75球或者3.75球
	if ($pankou_start == 3 && ($pankou_half_end == 2.75 || $pankou_half_end == 3.75) && $over_start > 1){
		return 5;
	}
	
	//6 比赛开始时间盘口为2.75球，水位大于等于0.9小于等于1，抓取时间2盘口为2.25球
	if ($pankou_start == 2.75 && $pankou_half_end == 2.25 && $over_start <= 1 && $over_start >= 0.9){
		return 6;
	}
	
	//10 比赛开始时间盘口为2.5球，水位大于1，抓取时间2盘口为2.25球，水位大于1
	if ($pankou_start == 2.5 && $pankou_half_end == 2.25 && $over_start > 1 && $over_half_end > 1){
		return 10;
	}
	
	//11 比赛开始时间盘口为2.5球，水位小于0.9，抓取时间2盘口为3.5球，水位大于1
	if ($pankou_start == 2.5 && $pankou_half_end == 3.5 && $over_start < 0.9 && $over_half_end > 1){
		return 11;
	}
	
	//15 比赛开始时间盘口为2.75球，抓取时间2盘口为4.5球
	if ($pankou_start == 2.75 && $pankou_half_end == 4.5){
		return 15;
	}
	*/
	
	return false;
}
